package com.example.wallet

import com.example.wallet.global.AppGlobals
import com.example.wallet.utils.createWalletChildFile
import com.example.wallet.utils.walletRootFileDirs
import java8.util.concurrent.CompletableFuture
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.web3j.crypto.Credentials
import org.web3j.crypto.Keys
import org.web3j.crypto.WalletUtils
import org.web3j.protocol.Web3j
import org.web3j.protocol.core.methods.response.Web3ClientVersion
import org.web3j.protocol.http.HttpService
import java.security.Security


class Web3jService {

    companion object {

        init {
            walletRootFileDirs()
            setupBouncyCastle()
        }

        //Web3j实例对象
        private val web3j: Web3j by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            Web3j.build(HttpService(AppGlobals.getApplication().getString(R.string.web3j_node)))
        }

        val WEB3J_SERVICE_INSTANCE: Web3jService by lazy {
            Web3jService()
        }

        private fun setupBouncyCastle() {
            val provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME)
                ?: return
            if (provider.javaClass == BouncyCastleProvider::class.java) {

                return
            }
            Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
            Security.insertProviderAt(BouncyCastleProvider(), 1)
        }


    }

    //版本信息
    /**
     * 外部通过 .thenAccept {
               Log.d("web3ClientVersion", "web3ClientVersion: ${it.web3ClientVersion}")
            }
     * */
    fun getVersion(): CompletableFuture<Web3ClientVersion> {

        val request = web3j.web3ClientVersion()

        return  request.sendAsync()
//        val send = request.sendAsync().thenAccept {  }
//
//        val web3Client = send.get()
//
//        return web3Client.web3ClientVersion
    }


//    //创建账户：
//    fun createCount(passWod: String): AccountInfo {
//
//        val file = createWalletChildFile()
//
//        val keyPair = Keys.createEcKeyPair()
//
//        return WalletUtils.generateWalletFile(passWod, keyPair, file, true)
//
////        val privateKey = keyPair.privateKey.toString(16)
////        val publicKey = keyPair.publicKey.toString(16)
////        val address = Keys.getAddress(keyPair)
////        return Triple(privateKey, publicKey, address)
//    }
//
//    fun saveKeysStore()
//
//
//    //加载KeysStore
//      fun loadKeysStore(passWod: String, filePath: String): Credentials {
//        return WalletUtils.loadCredentials(passWod, filePath)
//    }
//
//    //获取账号
//    fun getAccountInfo(passWod: String, filePath: String): AccountInfo {
//        val mCredentials = loadCredential(passWod, filePath)
//        val mKeyPair = mCredentials.ecKeyPair
//        return AccountInfo(
//            mKeyPair.publicKey.toString(16),
//            Keys.getAddress(mKeyPair),
//            mKeyPair.privateKey.toString(16)
//        )
//    }
//
//    fun createCountRandom(){
////        val mnemonic: String = ChainUtil.genMnemonic()
//    }
}